package neu.soft.facade;

import lombok.extern.slf4j.Slf4j;
import neu.soft.entity.DO.ReceiptJournal;
import neu.soft.entity.FinancialVoucher;
import neu.soft.entity.Invoice;
import neu.soft.entity.PayableOrder;
import neu.soft.exception.AppException;
import neu.soft.exception.AppExceptionCodeMsg;
import neu.soft.service.FinancialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 李博
 * 核算交易门面
 */
@Component
@Slf4j
public class ReceiptFacade {

    @Autowired
    private ReceiptAccountingHandler receiptAccountingHandlerFacade;

    @Autowired
    private ReceiptSourceHandler receiptSourceHandler;

    @Autowired
    private FinancialService financialService;

    //处理出库明细进行成本核算
    public void receiptAccountingHandle(String outOrderId, List<String> code, List<Integer> num, String oid) {

        receiptAccountingHandlerFacade.accountingOrder(outOrderId, code, num, oid);
    }

    //客户支付付款，记录交易
    public void journal(ReceiptJournal receiptJournal){
        receiptSourceHandler.addJournalService(receiptJournal);
    }

    //订单完成，核算到账处理，并且录入结账凭证
    public void generateSourceDocument(String oid){
        FinancialVoucher financialVoucher = receiptSourceHandler.generateSourceDocument(oid);
        if ("0".equals(financialVoucher.getVoucherType())){
            throw new AppException(AppExceptionCodeMsg.YYDZ_JDWGXSB);
        }
        BigDecimal price = financialVoucher.getPrice();
        financialService.receipt(price);


    }
    //采购管理核算处理
    public FinancialVoucher supplyAccountingHandle(PayableOrder payableOrder, Invoice invoice){
        return receiptAccountingHandlerFacade.accountingSupply(payableOrder,invoice);
    }


}
